﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using System.Data;

namespace PeisongWeb
{
    public partial class iframe_buyer_favourite : System.Web.UI.Page
    {
        protected int week
        {
            get
            {
                int tmp;
                try { tmp = Convert.ToInt32(ViewState["week"]); }
                catch { tmp = 0; }
                return tmp;
            }
            set
            { ViewState["week"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["week"] != null)
                {
                    if (Library.Number.IsIntPositive(Request.QueryString["week"]))
                    {
                        week = Convert.ToInt32(Request.QueryString["week"]);

                        string[] weeks = { "一", "二", "三", "四", "五", "六", "日" };

                        lbl_Weekday.Text = string.Concat("星期", weeks[week - 1]);
                    }
                }
                rptBind();
            }
        }


        void rptBind()
        {
            //int companyid = logic.customer.getById(logic.customer.userId).CompanyId;

            if (week != 0)
            {
                int companyId = logic.customer.getCompanyId();
                CompanyWeekEntity companyWeekEntity = logic.companyWeek.getByWeek(week, companyId);

                DataTable dt = logic.companyWeek.getByProducts(companyWeekEntity.Products);
                rpt_list.DataSource = dt;
                rpt_list.DataBind();
            }
            else
                rpt_list.DataBind();
        }

        protected void rpt_list_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                int proId = Convert.ToInt32(e.CommandArgument);
                delProducts(proId);
                rptBind();
            }
        }

        void delProducts(int productId)
        {
            CompanyWeekEntity cw = new CompanyWeekEntity();
            int companyId = logic.customer.getCompanyId();
            cw = logic.companyWeek.getByWeek(week, companyId);
            if (cw.CsId != 0)
            {
                string cw_pros = "";
                string[] splitstr = { "," };
                string[] pros = cw.Products.Split(splitstr, StringSplitOptions.RemoveEmptyEntries);



                for (int i = 0; i < pros.Length; i++)
                {
                    if (pros[i] != productId.ToString())
                    {
                        cw_pros += pros[i] + ",";
                    }
                }
                cw.Products = cw_pros;
                logic.companyWeek.update(cw);
            }

        }
    }
}
